﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class TeamDetails : System.Web.UI.Page
    {
        AppLib.Team team;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }

            int id = int.Parse(Server.HtmlEncode(Request.Cookies["TeamDetail"].Value));
            team = MidApp.ManageTeam.GetTeamByID(id);
            ImageAvatar2.DefaultImage = team.ImageURL;
            ImageAvatar2.Email = team.TeamID.ToString();

        }
        protected void Page_PreRender(object sender, EventArgs e)
        {            
            SetTeamInfo();

            SetRepeaters();
            IsUserLeader();

            ButtonRequest.Visible = true;
            ButtonStatistics.Visible = false;

            if (MidApp.ManageIsMember.IsTeamLeader(team.TeamID, Common.GetLoggedUser(this.Page).UserID) || MidApp.ManageIsMember.IsTeamTreasurer(team.TeamID, Common.GetLoggedUser(this.Page).UserID))
            {
                ButtonStatistics.Visible = true;
            }

            if (MidApp.ManageIsMember.IsTeamLeader(team.TeamID, Common.GetLoggedUser(this.Page).UserID))
            {
                ButtonRequest.Visible = false;
            }
            else
            {
                if (!MidApp.ManageIsMember.IsTeamMember(team.TeamID, Common.GetLoggedUser(this.Page).UserID))
                {
                    ButtonRequest.Visible = false;
                }
                else
                { 
                    if (!MidApp.ManagePendingRequest.IsTeamApplicant(team.TeamID, Common.GetLoggedUser(this.Page).UserID))
                    {
                        ButtonRequest.Visible = false;
                    }
                }
            }
            if (MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID))
                ButtonRequest.Visible = false;

        }

        private void SetRepeaters()
        {

            RepeaterTeamApplicants.DataSource = MidApp.ManagePendingRequest.GetAllApplicants(team.TeamID);
            RepeaterTeamApplicants.DataBind();

            RepeaterTeamLeaders.DataSource = MidApp.ManageIsMember.GetAllLeaders(team.TeamID);
            RepeaterTeamLeaders.DataBind();

            RepeaterTeamMembers.DataSource = MidApp.ManageIsMember.GetAllSimpleMembers(team.TeamID);
            RepeaterTeamMembers.DataBind();

        }
        protected void SetTeamInfo()
        {
            ImageAvatar2.DefaultImage = team.ImageURL;
            LabelTeamName.Text = team.TeamName;
            LabelLocation.Text = team.Location;
            LabelDateOfFoundtaion.Text = team.DateOfFoundation.ToShortDateString().ToString();
            LabelDescription.Text = team.Description;
            LabelLeader.Text = (MidApp.ManageUser.GetUserByID(team.LeaderID)).Nick;

        }
        //metoda vraci nick hrace zadajiciho o vstup do tymu
        protected string GetUserNick(object sender)
        {
            var aux = (AppLib.PendingRequest)sender;

            return MidApp.ManageUser.GetUserByID(aux.UserID).Nick;
        }
        //metoda vraci nick clena tymu
        protected string GetMemberNick(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return MidApp.ManageUser.GetUserByID(aux.UserID).Nick;
        }
        //metoda vraci string hodnotu ID uzivatele
        protected string GetUserID(object sender)
        {
            var aux = (AppLib.PendingRequest)sender;

            return aux.UserID.ToString();
        }
        //metoda vraci string hodnotu ID leadera
        protected string GetLeaderID(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return aux.UserID.ToString();
        }
        //metoda vraci string hodnotu ID clena
        protected string GetMemberID(object sender)
        {
            var aux = (AppLib.IsMember)sender;

            return aux.UserID.ToString();
        }

        protected string GetMemberAvatar(object sender)
        {
            try
            {
                var pom = (AppLib.IsMember)sender;
                return MidApp.ManageUser.GetUserImage(pom.UserID);
            }
            catch (InvalidCastException)
            {
                var pom = (AppLib.PendingRequest)sender;
                return MidApp.ManageUser.GetUserImage(pom.UserID);
            }

        }
        protected string GetMemberEmail(object sender)
        {
            try
            {
                var pom = (AppLib.IsMember)sender;
                return (MidApp.ManageUser.GetUserByID(pom.UserID)).PrimaryEmail;
            }
            catch (InvalidCastException)
            {
                var pom = (AppLib.PendingRequest)sender;
                return (MidApp.ManageUser.GetUserByID(pom.UserID)).PrimaryEmail;
            }

        }

        //obsluha tlacitka pro zobrazeni detailu hrace
        protected void LinkButtonUserInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("UserDetail.aspx");
        }
        //obsluha tlacitka pro prijeti hrace do tymu
        protected void LinkButtonApprove_Click(object sender, EventArgs e)
        {

            var aux = int.Parse(((LinkButton)sender).CommandArgument);

            MidApp.ManagePendingRequest.DeleteRecord(team.TeamID, aux);
            MidApp.ManageIsMember.ApproveUserToTeam(aux, team.TeamID,false,false);

        }
        //obsluha tlacitka pro odmitnuti hrace
        protected void LinkButtonDisApprove_Click(object sender, EventArgs e)
        {
            var aux = int.Parse(((LinkButton)sender).CommandArgument);
            MidApp.ManagePendingRequest.DeleteRecord(team.TeamID, aux);
        }
        //metoda zjistuje, zda je user vedoucim tymu, podle toho se zobrazi aplikacni repeater pro rpijeti do tymu
        protected void IsUserLeader()
        {
            if (MidApp.ManageIsMember.IsTeamLeader(team.TeamID, Common.GetLoggedUser(this.Page).UserID))
                PanelApplications.Visible = true;
        }

        protected void ButtonRequest_Click(object sender, EventArgs e)
        {
            MidApp.ManagePendingRequest.ApplyUserToTeam(Common.GetLoggedUser(this.Page).UserID, team.TeamID);
        }

        protected void ButtonStatistics_Click(object sender, EventArgs e)
        {
            Response.Redirect("TeamStatistics.aspx");
        }
    }
}